import { user_login_staticNode } from './cla_user_login_staticNode';
import { user_login_eventBind } from './cla_user_login_eventBind';
import {
  placeholder_user_logined,
  spacename_user_login,
} from '../DataDefine/sourceData';
import type { user_login_props } from './cla_user_login_props';
import { listenEvent_mana } from '../../../../global_comps/Classes/listenEvent_mana/Classes/cla_listenEvent_mana';
import { res_user_login_info } from './api/cla_res_user_login_info';
import { api_user_login_info } from './api/cla_api_user_login_info';
import { api_port } from '../../../../global_comps/api_config';
import { high_light_dom } from '../../../../global_comps/Func/high_light_dom';
import { code_login } from './api/Types';
import { location_href } from '../../../../../global/Func/location_href';
import { settimeNth_anima_check } from '../../../../../global/Func/settimeNth';
import { inject_remove_doc_per_delay } from '../../../../global_comps/DataDefine/sourceData';
import { inject_chi, remove_chi } from '../../../../global_comps/Func';
import { remove_inp_val } from '../../../../global_comps/Func/remove_inp_val';
import { remove_class } from '../../../../global_comps/Func/remove_class';
import {
  css_child_margin_top,
  css_cursor_pointer,
} from '../../../../global_comps/Css';
import { sleep } from '../../../../../global/Func/sleep';
import { inject_dom_func_promise } from '../../../../global_comps/Func/inject_dom';
import { disappearEl_child_func_promise } from '../../../../global_comps/Func/disappear_dom';
import { set_localstorage_login_user_identity } from '../../../../global_comps/api_config/localstorage_login_user_identity';

const user_login_dataManaTag = 'user_login_dataMana';

export class user_login_dataMana extends user_login_staticNode {
  get [Symbol.toStringTag]() {
    return user_login_dataManaTag;
  }

  constructor(props: user_login_props) {
    super(props);
  }

  /**
   * 事件 绑定 this
   */
  eventBind = new user_login_eventBind();

  /**
   * 事件 监听管理
   */
  eventM = new listenEvent_mana(spacename_user_login);

  /**
   * 检测 通过
   */
  check_over = true;

  /**
   * 未通过 检测 的 doc 元素 集
   */
  disactive_dom_arr: HTMLElement[] = [];

  /**
   * 注册 信息返回 结果
   */
  res_info: res_user_login_info = undefined as any;

  /**
   * 信息集
   */
  weakmap_info = new WeakMap<HTMLInputElement, any>();

  /**
   * 装载 信息
   */
  set_cur_dom_val(dom: HTMLInputElement) {
    this.weakmap_info.set(dom, dom.value);
  }

  /**
   * 获取 信息
   */
  get_cur_dom_val(dom: HTMLInputElement) {
    return this.weakmap_info.get(dom);
  }

  /**
   * 获取 注册 信息
   */
  get_all_sign_info() {
    let ret = new api_user_login_info();
    ret.account = this.get_cur_dom_val(this.inp_account);

    ret.passworld = this.get_cur_dom_val(this.inp_passworld);

    return ret;
  }

  /**
   * 高亮 提示 未通过 doc 元素 集
   */
  #check_disactive_dom() {
    for (const cur_dom of this.disactive_dom_arr) {
      high_light_dom(cur_dom);
    }
  }

  /**
   * 移除
   *
   * login 注册 信息表
   */
  #remove_login_dom() {
    let p_arr = [];

    let cur_i = 0;

    let cur_dom: HTMLElement;

    /**
     * 忽略 移出 项
     */
    let ignore_remove_arr: HTMLElement[] = [];

    for (cur_dom of this.dom_border.children as any) {
      if (ignore_remove_arr.includes(cur_dom)) {
        continue;
      }
      cur_i++;
      let p = new Promise<void>((resolve) => {
        settimeNth_anima_check(
          this.dom_border,
          cur_i * inject_remove_doc_per_delay,
          remove_chi(cur_dom, resolve)
        );
      });

      p_arr.push(p);
    }

    return Promise.all(p_arr);
  }

  /**
   * 注册 失败
   *
   * 回退 注册表 doc
   */
  #inject_login_dom() {
    let p_arr = [];

    let cur_i = 0;

    let cur_dom: HTMLElement;

    /**
     * 忽略 移出 项
     */
    let ignore_inject_arr: HTMLElement[] = [this.dom_logined];

    for (cur_dom of this.dom_border.children as any) {
      if (ignore_inject_arr.includes(cur_dom)) {
        continue;
      }
      cur_i++;
      let p = new Promise<void>((resolve) => {
        settimeNth_anima_check(
          this.dom_border,
          cur_i * inject_remove_doc_per_delay,
          inject_chi(cur_dom, resolve)
        );
      });

      p_arr.push(p);
    }

    return Promise.all(p_arr);
  }

  async #inject_logined_dom() {
    // 移除 子 节点 top
    remove_class(this.dom_border, css_child_margin_top);

    // 添加 手指
    remove_class(this.dom_border, css_cursor_pointer, true);

    await inject_dom_func_promise(this.dom_logined);
  }

  /**
   * 账户 未 查询到
   */
  #reject_account_operate() {
    // 移除 所有 inp 值
    let remova_val_arr = [this.inp_account, this.inp_passworld];

    for (const cur_dom of remova_val_arr) {
      remove_inp_val(cur_dom);
    }

    // 高亮 账号 元素
    high_light_dom(this.inp_account.parentNode as any);
  }

  /**
   * 密码 未匹配
   */
  #reject_passworld_operate() {
    // 移除 所有 inp 值
    let remova_val_arr = [this.inp_passworld];
    for (const cur_dom of remova_val_arr) {
      remove_inp_val(cur_dom);
    }

    // 高亮 密码 元素
    high_light_dom(this.inp_passworld.parentNode as any);
  }

  /**
   * 提交 用户 登录 请求
   */
  async #api_login() {
    let login_info = this.get_all_sign_info();
    console.log('提交了', login_info);

    let res = await api_port.post('/my_schedule/user/login', login_info);

    this.res_info = new res_user_login_info(res.data);

    console.log(this.res_info);
  }

  /**
   * 接受 返回 结果
   *
   * 执行 动画
   */
  async #res_anima() {
    // 登录 成功
    if (this.res_info.res_code == code_login.access) {
      console.log(placeholder_user_logined);

      // 存用户 唯一身份
      set_localstorage_login_user_identity(this.res_info.identity);

      await this.#inject_logined_dom();

      await sleep(1000);

      await disappearEl_child_func_promise(this.dom_border);

      this.#Path_user();
      return;
    }

    await this.#inject_login_dom();

    if (this.res_info.res_code == code_login.account_reject) {
      this.#reject_account_operate();
      return;
    }

    if (this.res_info.res_code == code_login.passworld_reject) {
      this.#reject_passworld_operate();
      return;
    }
  }

  /**
   * 提交 用户 登录 信息
   */
  async submit_info() {
    if (!this.check_over) {
      this.#check_disactive_dom();
      return;
    }

    let p_arr = [this.#remove_login_dom(), this.#api_login()];

    await Promise.all(p_arr);

    await this.#res_anima();
  }

  /**
   * 跳转 并记录 用户 信息
   */
  #Path_user() {
    // 跳转到 用户页面
    location_href('');
  }
}
